package yuejian_07.yuejian_07.model;



import java.util.List;

import retrofit2.Callback;
import retrofit2.Response;
import yuejian_07.yuejian_07.RetrofitListener;
import yuejian_07.yuejian_07.activity.ActSearchActivity;
import yuejian_07.yuejian_07.base.RetrofitListenerTow;
import yuejian_07.yuejian_07.javabean.Activities;
import yuejian_07.yuejian_07.javabean.ActivityOrder;
import yuejian_07.yuejian_07.service.ActivityService;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static yuejian_07.yuejian_07.base.Constant.BASE_URL;


public class ActivityModel {
    private Retrofit retrofit;
    public ActivityModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    //获取活动列表
    public void astivitylist( final RetrofitListener listener){
        ActivityService activityService=retrofit.create(ActivityService.class);
        Call call=activityService.getAllActivities();
        Callback<List<Activities>> callback=new Callback<List<Activities>>(){
            @Override
            public void onResponse(Call<List<Activities>> call, Response<List<Activities>> response ){
                List<Activities> data=response.body();
                listener.onSuccess(data);
            }
            @Override
            public  void onFailure(Call<List<Activities>> call,Throwable t){
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    //获取活动详情
    public void getActivityById(int Activity_id, final RetrofitListener listener){
        ActivityService activityService=retrofit.create(ActivityService.class);
        Call call=activityService.getActivityById(Activity_id);
        Callback<Activities> callback=new Callback<Activities>(){
            @Override
            public void onResponse(Call<Activities> call, Response<Activities> response){

                listener.onSuccess(response.body());
            }
            @Override
            public void onFailure(Call<Activities> call,Throwable t){
                t.printStackTrace();
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }
    public void getActBySearch(String SearchKey, final ActSearchActivity listener){
        ActivityService activityService=retrofit.create(ActivityService.class);
        Call call=activityService.getActBySearch(SearchKey);
        Callback<List<Activities>> callback=new Callback<List<Activities>>(){
            @Override
            public void onResponse(Call<List<Activities>> call, Response<List<Activities>> response ){
                List<Activities> data=response.body();
                listener.onSuccess(data);
            }
            @Override
            public  void onFailure(Call<List<Activities>> call, Throwable t){
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    //活动下订单
    public void insertOrder(int user_id,int activity_id, final RetrofitListenerTow listener){
        ActivityService activityService=retrofit.create(ActivityService.class);
        Call call=activityService.insertOrder(user_id,activity_id);
        Callback<ActivityOrder> callback=new Callback<ActivityOrder>(){
            @Override
            public void onResponse(Call<ActivityOrder> call, Response<ActivityOrder> response ){
                ActivityOrder data=response.body();
                listener.onSuccessTow(data);
            }
            @Override
            public  void onFailure(Call<ActivityOrder> call, Throwable t){
                listener.onFailTow();
            }
        };
        call.enqueue(callback);
    }
}
